package com.tim.app.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.application.library.log.DLOG;
import com.google.gson.Gson;
import com.netease.nimlib.sdk.NimIntent;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.CustomNotification;
import com.tim.app.R;
import com.tim.app.netease.cache.CustomNotificationCache;
import com.tim.app.server.entry.Notice;
import com.tim.app.server.entry.User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.util.ToastUtil;
import com.tim.app.util.ViewGT;

/**
 * 自定义通知消息广播接收器
 */
public class CustomNotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = context.getPackageName() + NimIntent.ACTION_RECEIVE_CUSTOM_NOTIFICATION;
        if (action.equals(intent.getAction())) {

            // 从intent中取出自定义通知
            CustomNotification notification = (CustomNotification) intent.getSerializableExtra(NimIntent.EXTRA_BROADCAST_MSG);
            Notice notice = new Gson().fromJson(notification.getContent(), Notice.class);
            if (!TextUtils.isEmpty(notice.getKick()) && UserManager.ins().isLogin() && UserManager.ins().getUserId().equals(notice.getKick())) {
                ToastUtil.showToast(context.getString(R.string.suspenedPrompt));
                UserManager.ins().logout(context);
            } else {
                if (notification.getSessionId().equals("servermoli")) {
                    notice.setNoticitifyContent(notification.getContent());
                    CustomNotificationCache.getInstance().addCustomNotification(notice);
                }
            }
            // 处理自定义通知消息
            DLOG.i("CustomNotificationReceiver", "receive custom notification: " + notification.getContent() + " from :" + notification.getSessionId() + "/" + notification.getSessionType());
        }
    }
}
